import { TOKEN, ISCLOSEAPPLYTEACHERSUCCESS } from '@/common/const';
import storage from '@/utils/storage';
import { Modal, message } from 'antd';
import { useEffect, useState } from 'react';

const cgjoyUrl = `${WEBCGDOMAIN}/member.php?mod=logging&action=login`;

export const useLoginUrl = () => {
  const [loginUrl, setLoginUrl] = useState('');
  useEffect(() => {
    const href = window.location.href;
    setLoginUrl(`${cgjoyUrl}&referer=${encodeURIComponent(href)}`);
  }, []);
  return loginUrl;
};
export const showLogin = ({
  title = '请先登录',
  okText = '立即登录',
  cancelText = '取消',
  onCancel = () => false as any,
}) => {
  const href = window.location.href;
  window.location.href = `${cgjoyUrl}&referer=${encodeURIComponent(href)}`;
  return;
  Modal.confirm({
    title,
    okText,
    cancelText,
    onOk: () => {
      window.location.href = `${cgjoyUrl}&referer=${encodeURIComponent(href)}`;
    },
    onCancel,
  });
};
export const useLoginStatus = () => {
  const [isLogin, setIsLogin] = useState<any>();

  useEffect(() => {
    const token = storage.get(TOKEN);
    setIsLogin(!!token?.value);
  }, []);
  return isLogin;
};

export const handleLogOut = async () => {
  const iframe = document.createElement('iframe');
  iframe.src = WEBCGDOMAIN + '/app.php?mod=user&do=logout&version=pc1.0';
  iframe.onload = () => {
    setTimeout(() => {
      message.success('退出成功');
      storage.remove(ISCLOSEAPPLYTEACHERSUCCESS);
      storage.remove(TOKEN);
      window.location.href = '/';
    }, 1000);
  };
  document.body.appendChild(iframe);
};
